﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class PhieuBanHangDAO
    {
        private static QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();

        public static List<PHIEUBANHANG> GetAllPhieuBanHang()
        {
            return db.PHIEUBANHANGs.ToList();
        }
        public static List<PHIEUBANHANG> SelectPhieuBanHang()
        {
            return db.PHIEUBANHANGs.ToList();
        }
        public static PHIEUBANHANG SelectPhieuBanHangByMaPhieuBanHang(PHIEUBANHANG pbh)
        {
            var query = db.PHIEUBANHANGs.Find(pbh.MaPhieuBanHang);
            if (query == null)
                return null;
            return query;
        }
        public static List<PHIEUBANHANG> SelectPhieuBanHangByNgayThanhToan(PHIEUBANHANG pbh)
        {
            var query = from d in db.PHIEUBANHANGs
                        where d.NgayThanhToan == pbh.NgayThanhToan
                        select d;
            return query.ToList();
        }
        public static void Insert(PHIEUBANHANG pbh)
        {
            db.PHIEUBANHANGs.Add(pbh);
            db.SaveChanges();
        }
        public static void Delete(PHIEUBANHANG pbh)
        {
            var query = db.PHIEUBANHANGs.Find(pbh.MaPhieuBanHang);
            if (query != null)
            {
                db.PHIEUBANHANGs.Remove(pbh);
                db.SaveChanges();
            }
        }
        public static void UpdateTienNo(PHIEUBANHANG pbh)
        {
            var query=db.PHIEUBANHANGs.Find(pbh);
            if (query != null)
            {
                query.SoTienNo = pbh.SoTienNo;
                db.SaveChanges();
            }
        }
        public static int GetLastMaPhieuBanHang()
        {
            var query = (from d in db.PHIEUBANHANGs
                        select d.MaPhieuBanHang).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
